/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sample.category;

import org.sadhar.sia.framework.ClassApplicationModule;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author hendro
 */
public class CategoryWnd extends ClassApplicationModule {

    private Listbox lstboxCategory;
    private Textbox txtboxSearch;

    private CategoryDAO catDAO;

    public CategoryWnd() {
        this.catDAO = new CategoryDAOImpl();
    }

    public void onCreate() throws Exception {
        lstboxCategory = (Listbox) getFellow("lstboxCategory");
        txtboxSearch = (Textbox) getFellow("txtboxSearch");
        load();
    }

    public void load() throws Exception {
        lstboxCategory.getItems().clear();
        int no = 1;
        for (Category cat : catDAO.list(txtboxSearch.getValue())) {
            Listitem item = new Listitem();
            item.setValue(cat);
            item.appendChild(new Listcell("" + no++));
            item.appendChild(new Listcell(cat.getName()));
            lstboxCategory.appendChild(item);
        }
    }

    public void lstboxCategoryOnSelect() throws Exception {
        Window windowProduct = (Window) this.getRoot();
        Textbox txtboxKeySearch = (Textbox)windowProduct.getFellow("txtboxKeySearch");
        Textbox txtboxIdCategory = (Textbox)windowProduct.getFellow("txtboxIdCategory");
        Category cat = (Category) lstboxCategory.getSelectedItem().getValue();
        txtboxKeySearch.setValue(cat.getName());
        txtboxIdCategory.setValue(cat.getId()+"");
        this.detach();        
    }
}
